package controller

import (
	"hello/service"
	"hello/util"
	"net/http"
	"strings"
)



func Upload(write http.ResponseWriter, request *http.Request)  {
	file, header, err := request.FormFile("file")
	if err != nil {
		util.RespFail(write, err.Error())
		return
	}

	fileType := request.FormValue("fileType")
	if len(fileType) <= 0  {
		filename := header.Filename
		strSlice := strings.Split(filename, ".")
		if len(strSlice) > 1 {
			fileType =  "." + strSlice[len(strSlice) - 1]
		}
	}

	attachService := service.AttachService{
		SrcFile:  file,
		FileType: fileType,
	}

	url, err := attachService.Dispath()
	if err != nil {
		util.RespFail(write, err.Error())
		return
	}

	util.RespOk(write, url, "")
}